﻿using System;
using System.Text.RegularExpressions;

namespace AMing.Extension.Net40
{
    public static class StringExtension
    {
        /// <summary>
        /// Json特殊字符过滤，参见http://www.json.org/
        /// </summary>
        /// <param name="sourceStr">要过滤的源字符串</param>
        /// <returns>返回过滤的字符串</returns>
        public static string JsonCharFilter(this string sourceStr)
        {
            sourceStr = sourceStr.Replace("\\", "\\\\");
            sourceStr = sourceStr.Replace("\b", "\\\b");
            sourceStr = sourceStr.Replace("\t", "\\\t");
            sourceStr = sourceStr.Replace("\n", "\\\n");
            sourceStr = sourceStr.Replace("\n", "\\\n");
            sourceStr = sourceStr.Replace("\f", "\\\f");
            sourceStr = sourceStr.Replace("\r", "\\\r");

            return sourceStr.Replace("\"", "\\\"");
        }

        /// <summary>
        /// 转成指定类型
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="val"></param>
        /// <param name="defaultval">转换失败的设置的值</param>
        /// <returns></returns>
        public static T To<T>(this string val, T defaultval = default(T)) where T : struct
        {
            try
            {
                if (string.IsNullOrWhiteSpace(val))
                {
                    return defaultval;
                }
                return (T)Convert.ChangeType(val, typeof(T), null);
            }
            catch (Exception)
            {
                return defaultval;
            }
        }

        /// <summary>
        /// 将指定字符串中的格式项替换为指定数组中相应对象的字符串表示形式。
        /// </summary>
        /// <param name="val"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string ToFromat(this string val, params object[] args)
        {
            return string.Format(val, args);
        }

        /// <summary>   
        /// 移除HTML标签   
        /// </summary>   
        /// <param name="HTMLStr">HTMLStr</param>   
        public static string RemoveHtmlTag(this string HTMLStr)
        {
            return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", "");
        }
        /// <summary>
        /// 过滤脚本
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string FilterScript(this string content)
        {
            string regexstr = @"(?i)<script([^>])*>(\w|\W)*</script([^>])*>";
            return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 获取XX与XX之间的字符串
        /// </summary>
        /// <param name="content"></param>
        /// <param name="s_val"></param>
        /// <param name="e_val"></param>
        /// <returns></returns>
        public static string GetAmongString(this string content, string s_val, string e_val)
        {
            string among = content;
            int index = among.ToLower().IndexOf(s_val);
            if (index < 0)
            {
                return null;
            }
            among = among.Substring(index + s_val.Length);
            index = among.IndexOf(e_val);
            if (index < 0)
            {
                return null;
            }
            among = among.Substring(0, index);

            return among;
        }

        /// <summary>
        /// 获取XX与XX之间的字符串
        /// </summary>
        /// <param name="content"></param>
        /// <param name="s_val"></param>
        /// <param name="e_val"></param>
        /// <param name="out_val"></param>
        /// <returns></returns>
        public static bool TryGetAmongString(this string content, string s_val, string e_val, out string out_val)
        {
            out_val = null;
            try
            {
                string among = content;
                int index = among.ToLower().IndexOf(s_val);
                if (index < 0)
                {
                    return false;
                }
                among = among.Substring(index + s_val.Length);
                index = among.IndexOf(e_val);
                if (index < 0)
                {
                    return false;
                }
                out_val = among.Substring(0, index);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
